package com.xydtech.ecard.custgroup.controller;

import com.xydtech.common.annotation.Log;
import com.xydtech.common.core.controller.BaseController;
import com.xydtech.common.core.domain.AjaxResult;
import com.xydtech.common.core.domain.entity.SysDept;
import com.xydtech.common.core.page.TableDataInfo;
import com.xydtech.common.enums.BusinessType;
import com.xydtech.common.utils.ShiroUtils;
import com.xydtech.common.utils.poi.ExcelUtil;
import com.xydtech.ecard.custgroup.domain.EcardCustomerGroupOrganize;
import com.xydtech.ecard.custgroup.domain.EcardCustomerOrganizeRelease;
import com.xydtech.ecard.custgroup.service.IEcardCustomerGroupOrganizeService;
import com.xydtech.ecard.custgroup.service.IEcardCustomerOrganizeReleaseService;
import com.xydtech.system.service.ISysDeptService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 社团组织信息Controller
 *
 * @author yuzhiguo
 * @date 2024-11-26 下午3:03
 */
@Controller
@RequestMapping("/ecard/customerGroup/organize")
public class EcardCustomerGroupOrganizeController extends BaseController
{
    private String prefix = "ecard/custgroup/organize";

    @Autowired
    private IEcardCustomerGroupOrganizeService ecardCustomerGroupOrganizeService;
    @Autowired
    private IEcardCustomerOrganizeReleaseService ecardCustomerOrganizeReleaseService;
    @Autowired
    private ISysDeptService sysDeptService;

    @RequiresPermissions("ecard:customerGroup:organize:view")
    @GetMapping()
    public String organize()
    {
        return prefix + "/organize";
    }

    /**
     * 查询社团组织信息列表
     */
    @RequiresPermissions("ecard:customerGroup:organize:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(EcardCustomerGroupOrganize ecardCustomerGroupOrganize)
    {
        String salt = getSalt();//盐值
        SysDept sysDept = sysDeptService.selectSecondLevelDeptByDeptId(ShiroUtils.getSysUser().getDeptId());
        ecardCustomerGroupOrganize.setDictDeptId(sysDept.getDeptId());
        startPage();
        List<EcardCustomerGroupOrganize> list = ecardCustomerGroupOrganizeService.selectEcardCustomerGroupOrganizeList(ecardCustomerGroupOrganize, salt);
        return getDataTable(list);
    }

    /**
     * 导出社团组织信息列表
     */
    @RequiresPermissions("ecard:customerGroup:organize:export")
    @Log(title = "社团组织信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(EcardCustomerGroupOrganize ecardCustomerGroupOrganize)
    {
        String salt = getSalt();//盐值
        SysDept sysDept = sysDeptService.selectSecondLevelDeptByDeptId(ShiroUtils.getSysUser().getDeptId());
        ecardCustomerGroupOrganize.setDictDeptId(sysDept.getDeptId());
        List<EcardCustomerGroupOrganize> list = ecardCustomerGroupOrganizeService.selectEcardCustomerGroupOrganizeList(ecardCustomerGroupOrganize, salt);
        ExcelUtil<EcardCustomerGroupOrganize> util = new ExcelUtil<EcardCustomerGroupOrganize>(EcardCustomerGroupOrganize.class);
        return util.exportExcel(list, "社团组织信息数据");
    }

    /**
     * 选择转交的客户经理
     */
    @RequiresPermissions("ecard:customerGroup:organize:handOver")
    @GetMapping("/handOver/{ids}")
    public String choiceUserByHand(@PathVariable("ids") String ids, ModelMap mmap) {
        mmap.put("ids", ids);
        return prefix + "/choiceUser";
    }

    /**
     * 社团组织信息转交
     */
    @Log(title = "社团组织信息转交客户经理", businessType = BusinessType.UPDATE)
    @PostMapping("/allocation")
    @ResponseBody
    public AjaxResult allocation(Long registId,Long deptId, Long[] ids, Long userId) {
        /**查询机构*/
        //SysUser user = sysUserService.selectUserById(userId);
        if(registId == null){
            return AjaxResult.error("请选择移交人员！");
        }
        if(deptId == null){
            return AjaxResult.error("请选择移交的机构！");
        }

        //移交社团组织信息列表
        List<EcardCustomerGroupOrganize> ecardCustomerGroupOrganizeList = new ArrayList<>();

        for (int i = 0; i < ids.length; i++) {
            //修改管户人id和创建人机构id
            EcardCustomerGroupOrganize ecardCustomerGroupOrganize = new EcardCustomerGroupOrganize();
            ecardCustomerGroupOrganize.setId(ids[i]);
            ecardCustomerGroupOrganize.setManagementPersonnelId(registId);
            ecardCustomerGroupOrganize.setDeptId(deptId);
            SysDept sysDept = sysDeptService.selectSecondLevelDeptByDeptId(deptId);
            ecardCustomerGroupOrganize.setCorpId(sysDept.getDeptId());//法人机构id
            ecardCustomerGroupOrganizeList.add(ecardCustomerGroupOrganize);
        }
        return toAjax(ecardCustomerGroupOrganizeService.allocationGroupOrganizeInfo(ecardCustomerGroupOrganizeList,registId, userId));
    }

    /**
     * 释放社团组织
     *
     * @author yuzhiguo
     * @date 2024/11/26 下午5:14
     */
    @RequiresPermissions("ecard:customerGroup:organize:handOver")
    @Log(title = "释放社团组织", businessType = BusinessType.UPDATE)
    @PostMapping("/release")
    @ResponseBody
    public AjaxResult release(Long[] ids) {
        String salt = getSalt();//盐值
        //释放社团组织列表
        List<EcardCustomerGroupOrganize> ecardCustomerGroupOrganizeList = new ArrayList<>();
        Long userId = ShiroUtils.getSysUser().getUserId();
        //新增社团组织释放记录
        List<EcardCustomerOrganizeRelease> ecardCustomerOrganizeReleaseList = new ArrayList<>();
        for (int i = 0; i < ids.length; i++) {
            //修改创建人id和创建人机构id
            EcardCustomerGroupOrganize ecardCustomerGroupOrganize = new EcardCustomerGroupOrganize();
            ecardCustomerGroupOrganize.setId(ids[i]);
            //释放时客户经理置为null
            ecardCustomerGroupOrganize.setManagementPersonnelId(null);
            ecardCustomerGroupOrganizeList.add(ecardCustomerGroupOrganize);

            EcardCustomerGroupOrganize customerGroupOrganize = ecardCustomerGroupOrganizeService.selectEcardCustomerGroupOrganizeById(ids[i], salt);//查询新型农业主体信息
            //新增释放记录
            EcardCustomerOrganizeRelease ecardCustomerOrganizeRelease = new EcardCustomerOrganizeRelease();
            ecardCustomerOrganizeRelease.setReleaseId(userId);//释放人用户id
            ecardCustomerOrganizeRelease.setOrganizeId(ids[i]);//社团组织id
            ecardCustomerOrganizeRelease.setBelongRegistId(customerGroupOrganize.getManagementPersonnelId());//社团组织归属人注册id
            ecardCustomerOrganizeReleaseList.add(ecardCustomerOrganizeRelease);
        }
        ecardCustomerOrganizeReleaseService.batchInsertEcardCustomerOrganizeRelease(ecardCustomerOrganizeReleaseList);//批量新增释放记录
        int i = ecardCustomerGroupOrganizeService.batchReleaseEcardCustomerGroupOrganize(ecardCustomerGroupOrganizeList);
        return toAjax(i);
    }

    /**
     * 启用/停用社团组织
     *
     * @author yuzhiguo
     * @date 2024/11/27 下午3:04
     */
    @RequiresPermissions("ecard:customerGroup:organize:switchOver")
    @Log(title = "启用/停用社团组织", businessType = BusinessType.UPDATE)
    @PostMapping("/switchOver")
    @ResponseBody
    public AjaxResult switchOver(Long[] ids, Integer state) {
        //社团组织列表
        List<EcardCustomerGroupOrganize> ecardCustomerGroupOrganizeList = new ArrayList<>();

        for (int i = 0; i < ids.length; i++) {
            //修改状态
            EcardCustomerGroupOrganize ecardCustomerGroupOrganize = new EcardCustomerGroupOrganize();
            ecardCustomerGroupOrganize.setId(ids[i]);
            ecardCustomerGroupOrganize.setState(state);
            ecardCustomerGroupOrganizeList.add(ecardCustomerGroupOrganize);
        }
        int i = ecardCustomerGroupOrganizeService.batchSwitchOverEcardCustomerGroupOrganize(ecardCustomerGroupOrganizeList);
        return toAjax(i);
    }

    /**
     * 新增社团组织信息
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存社团组织信息
     */
    @RequiresPermissions("ecard:customerGroup:organize:add")
    @Log(title = "社团组织信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(EcardCustomerGroupOrganize ecardCustomerGroupOrganize)
    {
        return toAjax(ecardCustomerGroupOrganizeService.insertEcardCustomerGroupOrganize(ecardCustomerGroupOrganize));
    }

    /**
     * 修改社团组织信息
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        String salt = getSalt();//盐值
        EcardCustomerGroupOrganize ecardCustomerGroupOrganize = ecardCustomerGroupOrganizeService.selectEcardCustomerGroupOrganizeById(id, salt);
        mmap.put("ecardCustomerGroupOrganize", ecardCustomerGroupOrganize);
        return prefix + "/edit";
    }

    /**
     * 修改保存社团组织信息
     */
    @RequiresPermissions("ecard:customerGroup:organize:edit")
    @Log(title = "社团组织信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(EcardCustomerGroupOrganize ecardCustomerGroupOrganize)
    {
        return toAjax(ecardCustomerGroupOrganizeService.updateEcardCustomerGroupOrganize(ecardCustomerGroupOrganize));
    }

    /**
     * 删除社团组织信息
     */
    @RequiresPermissions("ecard:customerGroup:organize:remove")
    @Log(title = "社团组织信息", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(ecardCustomerGroupOrganizeService.deleteEcardCustomerGroupOrganizeByIds(ids));
    }
}
